package com.muyu.wechat.util;

import okhttp3.*;

import java.io.IOException;
/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: YZL
 * @Created: 2024/9/19 20:26
 */
public class OkHttpUtils {

    private static final OkHttpClient client = new OkHttpClient();

    public static String sendGetRequest(String urlString) {
        Request request = new Request.Builder()
                .url(urlString)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String sendPostRequest(String urlString, String params) {
        RequestBody requestBody = RequestBody.create(params, MediaType.parse("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(urlString)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
